package patterndemo;


import lombok.Data;


@Data
public class PurchaseRequest {

    /**
     * 采购金额
     */
    private double amount;
    /**
     * 采购单编号
     */
    private int number;

    /**
     * 采购目的
     */
    private String purpose;


    public PurchaseRequest(double amount, int number, String purpose) {
        this.amount = amount;
        this.number = number;
        this.purpose = purpose;
    }
}


/**
 * 审批者类：抽象处理类
 */
abstract class Approver {

    /**
     * 后继对象
     */
    protected Approver successor;

    protected String name;

    public Approver(String name) {
        this.name = name;
    }

    public void setSuccessor(Approver successor) {
        this.successor = successor;
    }

    /**
     * 抽象请求处理方法
     *
     * @param request
     */
    public abstract void processRequest(PurchaseRequest request);
}

/**
 * 主任类：具体处理者
 */
class Director extends Approver {

    public Director(String name) {
        super(name);
    }

    /**
     * 具体请求处理方法
     *
     * @param request
     */
    @Override
    public void processRequest(PurchaseRequest request) {
        if (request.getAmount() < 50000) {
            System.out.println("主任" + this.name + "审批采购单:" + request.getNumber()
                    + ",金额：" + request.getAmount() + "元，采购目的：" + request.getPurpose());
        } else {
            //转发请求
            this.successor.processRequest(request);
        }
    }
}

/**
 * 副董事长类：具体处理者
 */
class VicePresident extends Approver {

    public VicePresident(String name) {
        super(name);
    }

    @Override
    public void processRequest(PurchaseRequest request) {
        if (request.getAmount() < 100000) {
            System.out.println("副董事长" + this.name + "审批采购单:" + request.getNumber()
                    + ",金额：" + request.getAmount() + "元，采购目的：" + request.getPurpose());
        } else {
            //转发请求
            this.successor.processRequest(request);
        }
    }
}

/**
 * 董事长
 */
class President extends Approver {

    public President(String name) {
        super(name);
    }

    @Override
    public void processRequest(PurchaseRequest request) {
        if (request.getAmount() < 500000) {
            System.out.println("董事长" + this.name + "审批采购单:" + request.getNumber()
                    + ",金额：" + request.getAmount() + "元，采购目的：" + request.getPurpose());
        } else {
            //转发请求
            this.successor.processRequest(request);
        }
    }
}

/**
 * 董事会
 */
class Congress extends Approver {


    public Congress(String name) {
        super(name);
    }

    @Override
    public void processRequest(PurchaseRequest request) {
        System.out.println("召开董事会审批采购单：" + this.name + "审批采购单:" + request.getNumber()
                + ",金额：" + request.getAmount() + "元，采购目的：" + request.getPurpose());

    }
}
